package com.yidu.view;

import com.google.gson.Gson;
import com.yidu.dao.TeacherMapper;
import com.yidu.domain.TClass;
import com.yidu.domain.Teacher;
import com.yidu.utils.SqlSessionTools;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>@Description: No Description</p>
 * <p>@Author yidu-liandyao</p>
 * <p>@Date: 2021-01-06</p>
 * <p>@Time: 10:06</p>
 */
public class TeacherTest {
    @Test
    public void selectAll(){
        /**
         * org.apache.ibatis.binding.BindingException: Type interface com.yidu.dao.TeacherMapper is not known to the MapperRegistry.
         */
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        List<Teacher> list = mapper.selectAll();
        for(Teacher teacher : list){
            System.out.println(teacher.getTid()+"    "+teacher.getName()+"    "+teacher.getPhone());
        }
        session.close();
    }


    @Test
    public void selectAll2(){
        /**
         * org.apache.ibatis.binding.BindingException: Type interface com.yidu.dao.TeacherMapper is not known to the MapperRegistry.
         */
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        List<Teacher> list = mapper.selectTeaAndCls();
        for(Teacher teacher : list){
            System.out.println("    "+teacher.getName()+"    "+teacher.getClsName());
        }
        session.close();
    }


    @Test
    public void selectAll3(){
        /**
         * org.apache.ibatis.binding.BindingException: Type interface com.yidu.dao.TeacherMapper is not known to the MapperRegistry.
         */
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        List<Teacher> list = mapper.selectAll2();
        Gson gson = new Gson();
        String json = gson.toJson(list);

        for(Teacher teacher : list){
            System.out.println("老师名字: "+teacher.getName()+"    "+teacher.getClsName());
            List<TClass> clsList = teacher.getTclass();
            for (TClass cls:clsList) {
                System.out.println("         班级名称:"+cls.getCname()+"  open:"+cls.getOpen());
            }
        }

        System.out.println(json);
        session.close();
    }

    @Test
    public void selectId(){
        /**
         * org.apache.ibatis.binding.BindingException: Type interface com.yidu.dao.TeacherMapper is not known to the MapperRegistry.
         */
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.findById(1);
        Gson gson = new Gson();
        String json = gson.toJson(teacher);
        System.out.println(json);

        session.close();
    }


    @Test
    public void add(){
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        List<Teacher> list = new ArrayList<>();
        Teacher teacher = new Teacher();
        teacher.setName("张三");
        teacher.setPhone("1233");
        teacher.setSex("男");
        teacher.setArea("湖南");
        list.add(teacher);
        list.add(teacher);
        list.add(teacher);
        mapper.addBatch(list);
        session.commit();
    }


    @Test
    public void update(){
        SqlSession session = SqlSessionTools.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        Teacher t = new Teacher();
        t.setName("张三");
        t.setTid(1);
        mapper.update(t);
    }
}
